package com.sen.pro.switches.admin.content.action;

import java.io.IOException;

import org.apache.commons.lang.xwork.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.sen.common.struts.BaseAction;
import com.sen.common.util.Feedback;
import com.sen.pro.switches.core.cfg.ENV;
import com.sen.pro.switches.core.common.Constants;
import com.sen.pro.switches.core.util.SiteFileUtil;

/**
 * html内容管理Action
 * 
 * @author tian qingsen
 */
@Scope("prototype")
@Controller("htmlContentAction")
@Namespace("/admin/content")
@InterceptorRefs(value = { @InterceptorRef(value = "switches-needsession-stack") })
public class HtmlContentAction extends BaseAction {

	private static final long serialVersionUID = -7684237300368931042L;
	private static Logger LOG = Logger.getLogger(HtmlContentAction.class);

	private String htmlDesc;
	private String htmlName;

	@Action(value = "html_manage", results = { @Result(name = SUCCESS, location = "/WEB-INF/view/admin/content/html_manage.jsp") })
	public String manage() {
		try {
			String htmlFile = ServletActionContext.getServletContext().getRealPath("/") + ENV.HTML_DIR + "/"
					+ htmlName;
			htmlDesc = SiteFileUtil.getSiteHtmlFileToStr(htmlFile);
		} catch (IOException e) {
			LOG.error("读取" + htmlName + ".html内容出错", e);
		}
		request.setAttribute("title", getHtmlTitle());
		return SUCCESS;
	}

	@Action("html_doedit")
	public String htmlDoEdit() {
		if (StringUtils.isBlank(htmlName)) {
			writeJson(Feedback.fail("非法的数据！"));
			return NONE;
		}
		if (StringUtils.isBlank(htmlDesc)) {
			writeJson(Feedback.fail("内容不能为空！"));
			return NONE;
		}
		// 保存文件信息
		try {
			String htmlFile = ServletActionContext.getServletContext().getRealPath("/") + ENV.HTML_DIR + "/"
					+ htmlName;
			SiteFileUtil.writeStrToSiteHtmlFile(htmlFile, htmlDesc);
			writeJson(Feedback.success("保存成功！"));
		} catch (IOException e) {
			writeJson(Feedback.fail("保存失败！"));
		}

		return NONE;
	}

	private String getHtmlTitle() {
		if (Constants.INTRODUCE_HTML_NAME.equalsIgnoreCase(htmlName)) {
			return "公司简介内容管理";
		} else if (Constants.SOLUTION_HTML_NAME.equalsIgnoreCase(htmlName)) {
			return "客制方案内容管理";
		} else if (Constants.WARRANTY_HTML_NAME.equalsIgnoreCase(htmlName)) {
			return "保固条款内容管理";
		}
		return StringUtils.EMPTY;
	}

	public String getHtmlDesc() {
		return htmlDesc;
	}

	public void setHtmlDesc(String htmlDesc) {
		this.htmlDesc = htmlDesc;
	}

	public String getHtmlName() {
		return htmlName;
	}

	public void setHtmlName(String htmlName) {
		this.htmlName = htmlName;
	}

}
